package xyz.robinbingo.leetcode.a20.b06.c22;

import java.util.stream.IntStream;

public class Solution {
    public boolean patternMatching(String pattern, String value) {
        int countA = 0, countB = 0;
        for (char ch : pattern.toCharArray()) {
            if (ch == 'a') {
                countA++;
            } else {
                countB++;
            }
        }
        if (countA < countB) {
            int temp = countA;
            countA = countB;
            countB = temp;
            char[] array = pattern.toCharArray();
            IntStream.range(0, array.length).forEach(i -> array[i] = array[i] == 'a' ? 'b' : 'a');
            pattern = new String(array);
        }
        if (value.length() == 0) {
            return countB == 0;
        }
        if (pattern.length() == 0) {
            return false;
        }
        for (int lenA = 0; countA * lenA <= value.length(); lenA++) {
            int rest = value.length() - countA * lenA;
            if ((countB == 0 && rest == 0) || (countB != 0 && rest % countB == 0)) {
                int lenB = (countB == 0 ? 0 : rest / countB);
                int pos = 0;
                boolean correct = true;
                String valueA = "", valueB = "";
                for (char ch : pattern.toCharArray()) {
                    if (ch == 'a') {
                        String sub = value.substring(pos, pos + lenA);
                        if (valueA.length() == 0) {
                            valueA = sub;
                        } else if (!valueA.equals(sub)) {
                            correct = false;
                            break;
                        }
                        pos += lenA;
                    } else {
                        String sub = value.substring(pos, pos + lenB);
                        if (valueB.length() == 0) {
                            valueB = sub;
                        } else if (!valueB.equals(sub)) {
                            correct = false;
                            break;
                        }
                        pos += lenB;
                    }
                }
                if (correct && !valueA.equals(valueB)) {
                    return true;
                }
            }
        }
        return false;
    }
}

